package com.example.droideep.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.droideep.common.dummydata.Placeholder;

/**
 * A placeholder fragment containing a simple view.
 */
public class PlaceholderFragment extends ListFragment {

    private Placeholder[] placeholders = Placeholder.PLACEHOLDERS;

    private OnFragmentListener mCallback;

    public interface OnFragmentListener {
        void startFragment(Fragment container, String name);
    }

    public PlaceholderFragment() {
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mCallback = (OnFragmentListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentListener");
        }
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        setListAdapter(new PlaceholderAdapter(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, placeholders));
    }

    private class PlaceholderAdapter extends ArrayAdapter<Placeholder> {

        public PlaceholderAdapter(Context context, int resource, int textViewResourceId, Placeholder[] placeholders) {
            super(context, resource, textViewResourceId, placeholders);
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        mCallback.startFragment(placeholders[position].container, placeholders[position].title);
    }
}